package com.mytime.util;

public class Time {

	private int hour;
	private int minute;
	private int second;
	
	/**
	 * Constructor with no parameter
	 * **/
	public Time() {
		this.hour = 0;
		this.second = 0;
		this.minute = 0;
	}

	/**
	 * Constructor
	 * **/
	public Time(String time) {
		String[] timeInfo = time.split(":");
		
		this.hour = Integer.parseInt(timeInfo[0]);
		this.minute = Integer.parseInt(timeInfo[1]);
		
		if (timeInfo.length == 3)
			this.second = Integer.parseInt(timeInfo[2]);
		else
			this.second = 0;
	}

	public void changeTime(String time) {
		String[] timeInfo = time.split(":");
		
		this.hour = Integer.parseInt(timeInfo[0]);
		this.minute = Integer.parseInt(timeInfo[1]);
		
		if (timeInfo.length == 3)
			this.second = Integer.parseInt(timeInfo[2]);
		else
			this.second = 0;
	}

	/**
	 *Returns the hour. 
	 * **/
	public int getHour() {
		return this.hour;
	}

	/**
	 *Changes the hour. 
	 * **/
	public void setHour(int hour) {
		this.hour = hour;
	}

	/**
	 *Returns the minute. 
	 * **/
	public int getMinute() {
		return this.minute;
	}
	
	/**
	 *Changes the minute. 
	 * **/
	public void setMinute(int minute) {
		this.minute = minute;
	}

	/**
	 * Returns the time in the format HH:mm
	 * **/
	public String getTime() {
		return String.format("%02d:%02d", this.getHour(), this.getMinute());
		
	}

	/**
	 * Returns the time in the format HH:mm:ss
	 * **/
	public String getCompleteTime() {
		return this.hour + ":" + this.minute + ":" + this.second;
	}

	/**
	 * Compares this object to a given Time object
	 * @return -1 if the time is smaller, 1 if it is greater and 0 if both times are equal. 
	 * **
	 */
	public int compareTo(Time t) {		
		//Compare hour
		if (t.getHour() > this.hour)
			return -1;
		else if (t.getHour() < this.hour)
			return 1;
		
		//Compare minute
		if (t.getMinute() > this.minute)
			return -1;
		else if (t.getMinute() < this.minute)
			return 1;
		
		return 0;
	}
	
	/**
	 * Adds to a given time.
	 * @return 
	 * **/
	public Time addTo(Time t) {		
		//Adds minutes
		int partialMinutes = this.getMinute() + t.getMinute();
		int additionalHour = 0; 
		
		if (partialMinutes > 60) {
			additionalHour += 1;
			partialMinutes = partialMinutes % 60;
		}

		//Adds hours
		int hour = this.getHour() + t.getHour();
		hour += additionalHour;
	
		//Updates time
		this.setHour(hour);
		this.setMinute(partialMinutes);
		
		return this;
	}
	
	/**
	 * Substract Time t2 from Time t1.
	 * **/
	public static Time subtract(Time t1, Time t2) {
		Time result = new Time();
		int t1Hour = t1.getHour();
		int t1Minutes = t1.getMinute();
		
		//TODO:
		//if (t1Hour < t2.getHour())
		//	return result;
					
		//Subtracts minutes
		if (t1Minutes < t2.getMinute()) {
			t1Hour -= 1;
			t1Minutes += 60;
		}
		result.setMinute(t1Minutes - t2.getMinute());
		
		//Subtracts hours
		result.setHour(t1Hour - t2.getHour());
		
		return result;
	}
}
